package org.jash.mvp2206.user;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.textfield.TextInputLayout;

import org.jash.mvp2206.R;
import org.jash.mvp2206.mvp.BaseActivity;

import java.util.List;

public class RegisterActivity extends BaseActivity<Construct.IRegisterView, RegisterPresenter> implements Construct.IRegisterView {
    private TextInputLayout username;
    private TextInputLayout password;
    private TextInputLayout phone;
    private TextInputLayout code;
    private TextInputLayout repeatPassword;
    private List<TextInputLayout> check;

    @Override
    protected int layoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initData() {
        presenter = new RegisterPresenter();
    }

    @Override
    protected void initView() {
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        phone = findViewById(R.id.phone);
        code = findViewById(R.id.code);
        repeatPassword = findViewById(R.id.repeat_password);
        check = List.of(phone, password, repeatPassword);
        findViewById(R.id.get_code).setOnClickListener(v -> {
            if (phone.isErrorEnabled()) {
                Toast.makeText(this, phone.getError(), Toast.LENGTH_SHORT).show();
                return;
            }
            presenter.getVerifyCode(phone.getEditText().getText().toString());
        });
        findViewById(R.id.check_code).setOnClickListener(v -> {
            presenter.checkCode(phone.getEditText().getText().toString(), code.getEditText().getText().toString());
        });
        findViewById(R.id.register).setOnClickListener(v -> {
            for (TextInputLayout layout : check) {
                if (layout.isErrorEnabled()) {
                    Toast.makeText(this, layout.getError(), Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            presenter.register(username.getEditText().getText().toString(), password.getEditText().getText().toString(), phone.getEditText().getText().toString());
        });
        phone.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().matches("1[35789]\\d{9}")) {
                    phone.setErrorEnabled(true);
                    phone.setError("手机号不合法");
                } else {
                    phone.setErrorEnabled(false);
                }
            }
        });
        password.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().matches(".{8,12}")) {
                    password.setErrorEnabled(true);
                    password.setError("密码为8-12位");
                } else if (!s.toString().matches("\\w+")) {
                    password.setErrorEnabled(true);
                    password.setError("密码只能有数字和字母");
                } else {
                    password.setErrorEnabled(false);
                }
            }
        });
        repeatPassword.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals(password.getEditText().getText().toString())) {
                    repeatPassword.setErrorEnabled(true);
                    repeatPassword.setError("两次密码不一致");
                } else {
                    repeatPassword.setErrorEnabled(false);
                }
            }
        });
    }

    @Override
    public void codeSuccess(int code) {
        this.code.getEditText().setText("" + code);
    }

    @Override
    public void checkedSuccess(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void registerSuccess(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        setResult(RESULT_OK, new Intent().putExtra("username", username.getEditText().getText().toString()).putExtra("password", password.getEditText().getText().toString()));
        finish();
    }

    @Override
    public void failure(Throwable t) {
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}